/*
 * @(#)FolderTypeTransformer.java
 * Date 2013-09-04
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.transformer.xds;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import oasis.names.tc.ebxml_regrep.xsd.rim._3.ClassificationType;
import oasis.names.tc.ebxml_regrep.xsd.rim._3.ExternalIdentifierType;
import oasis.names.tc.ebxml_regrep.xsd.rim._3.RegistryPackageType;
import oasis.names.tc.ebxml_regrep.xsd.rim._3.SlotType1;

import org.joda.time.DateTime;

import us.hornerscorners.lamppost.exception.ServiceActionException;

import us.hornerscorners.lamppost.model.document.Folder;

import us.hornerscorners.lamppost.registry.constant.Metadata;
import us.hornerscorners.lamppost.registry.constant.SlotName;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-09-04
 * @author         Jim Horner
 */
@ApplicationScoped
public class FolderTypeTransformer extends AbstractTransformer<Folder, RegistryPackageType> {

    /** Field description */
    @Inject
    private ClassificationTypeTransformer classificationTransformer;

    /** Field description */
    @Inject
    private ExternalIdentifierTypeTransformer externalIdentifierTransformer;

    /** Field description */
    @Inject
    private InternationalStringTypeTransformer internationalStringTransformer;

    /** Field description */
    @Inject
    private SlotTypeTransformer slotTransformer;

    /** Field description */
    @Inject
    private VersionInfoTypeTransformer versionInfoTransformer;

    /**
     * Method description
     *
     *
     * @param folder
     *
     * @return
     */
    @Override
    public RegistryPackageType transform(Folder folder) throws ServiceActionException {

        RegistryPackageType result = new RegistryPackageType();

        // ***********
        // attributes

        result.setId(folder.getEntryURN());

        // versioning
        result.setLid(folder.getEntryLogicalURN());
        result.setVersionInfo(
            this.versionInfoTransformer.transform(folder.getEntryVersion()));

        result.setStatus(folder.getAvailabilityStatus().getCode());
        result.setObjectType(Metadata.RegistryPackageObjectType.getURN());

        // *******
        // slots
        List<SlotType1> slots = result.getSlot();

        DateTime lastUpdateTime = folder.getLastUpdateTime();

        if (lastUpdateTime != null) {

            slots.add(this.slotTransformer.transform(SlotName.LastUpdateTime,
                    lastUpdateTime));
        }

        // ************
        // extra slots

        slots.addAll(this.slotTransformer.transform(folder.getExtraAttributes()));

        // *********
        // elements

        result.setName(
            this.internationalStringTransformer.transform(folder.getDisplayNames()));

        result.setDescription(
            this.internationalStringTransformer.transform(folder.getDescriptions()));

        // **************
        // classification

        List<ClassificationType> classifications = result.getClassification();

        // folder classification
        classifications.add(
            this.classificationTransformer.transformClassificationNode(
                folder.getClassificationNode(), folder));

        // codes

        classifications.addAll(
            this.classificationTransformer.transformCodedElements(
                Metadata.FolderCodeList, folder, folder.getCodes()));

        // *********************
        // external identifiers

        List<ExternalIdentifierType> externalIds = result.getExternalIdentifier();

        externalIds.add(
            this.externalIdentifierTransformer.transformPatientIdentifier(
                Metadata.FolderPatientId, folder, folder.getPatientId()));

        externalIds.add(
            this.externalIdentifierTransformer.transformUniqueIdentifier(
                Metadata.FolderUniqueId, folder, folder.getUniqueId()));

        return result;
    }
}
